package com.cneiu.ppms.web.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import com.cneiu.ppms.web.base.BaseService;
import com.cneiu.ppms.web.model.LineModel;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
/**
 * 供电线路业务
 * @author zhaokf
 * @date 2013-04-19
 */
public class LineService extends BaseService
{
	/**
	 * 将机构数据转化成Tree格式(递归实现)
	 * @param lines
	 * @return
	 */
	public static List<HashMap<Object,Object>> treeComboBox(LineModel model){
		List<HashMap<Object,Object>> smap = new ArrayList<HashMap<Object,Object>>();
		/**查询本节点下面的子节点**/
		String query_sql = "SELECT * FROM G_LINE WHERE LINE_PARENT=?";
		List<LineModel> lines = LineModel.dao.find(query_sql,model.get("LINE_CODE"));
		/**组装本节点数据**/
		HashMap<Object, Object> map = new HashMap<Object, Object>();
		map.put("line_code", model.get("LINE_CODE"));
		map.put("line_name", model.get("LINE_NAME"));
		map.put("treeText", model.get("LINE_CODE")+" "+model.get("LINE_NAME"));
		smap.add(map);
		/**判断是否存在子节点**/
		if(lines!=null && lines.size()>0){
			HashMap<Object, Object> map1 =  null;
			/**循环子节点**/
			for(LineModel line:lines){
					map1 = new HashMap<Object, Object>();
					map1.put("line_code", line.get("LINE_CODE"));
					map1.put("line_name", line.get("LINE_NAME"));
					map1.put("treeText", line.get("LINE_CODE")+" "+line.get("LINE_NAME"));
					smap.add(map1);
					/***递归查找*/
					treeComboBox(line);
			}
		}
		return smap;
	}

	
	/**
	 * 将机构数据转化成Tree格式(递归实现)
	 * @param lines
	 * @return
	 */
	public static HashMap<Object,Object> treeList(LineModel model){
		/**组装本节点数据**/
		HashMap<Object, Object> map = new HashMap<Object, Object>();
		map.put("line_code", model.get("LINE_CODE"));
		map.put("line_name", model.get("LINE_NAME"));
		/**查询本节点下面的子节点**/
		String query_sql = "SELECT * FROM G_LINE WHERE LINE_PARENT=?";
		List<LineModel> lines = LineModel.dao.find(query_sql,model.get("LINE_CODE"));
		List<HashMap<Object,Object>> smap = new ArrayList<HashMap<Object,Object>>();
		/**判断是否存在子节点**/
		if(lines!=null && lines.size()>0){
			map.put("leaf", false);
			/**循环子节点**/
			for(LineModel line:lines){
					HashMap<Object, Object> map1 = new HashMap<Object, Object>();
					map1.put("line_code", line.get("LINE_CODE"));
					map1.put("line_name", line.get("LINE_NAME"));
					List<LineModel> lineslist = LineModel.dao.find(query_sql,line.get("LINE_CODE"));
					if(lineslist!=null && lineslist.size()>0){
						map1.put("leaf", false);
					}else{
						map1.put("leaf", true);
					}
					smap.add(map1);
					/***递归查找*/
					treeList(line);
			}
			/**添加子节点数据**/
			map.put("children", smap);
		}else{
			map.put("leaf", true);
		}
		return map;
	}
	/**
	 * 分页列出供电线路
	 * @param page
	 * @param limit
	 * @param search
	 * @return
	 */
	public static Page<LineModel> list(Map<String,String> params){
		JSONObject search = JSONObject.fromObject(params.get("search"));
		int limit = 0;
		int page = 1;
		try{
			limit = params.get("limit")==null?0:Integer.parseInt(params.get("limit").toString());
			page = params.get("page")==null?1:Integer.parseInt(params.get("page").toString());
		}catch (Exception e){
			limit = 100;
			page = 1;
		}
		String where = "";
		try{
			if (search.get("line_code") != null){
				where = " AND line_code=" + search.get("line_code");
			}
			if (search.get("line_name") != null){
				where = " AND line_name LIKE '%" + search.get("line_name") + "%'";
			}
			where = " WHERE " + where.substring(5);
		}catch (Exception e){
		}
		Page<LineModel> Subs = null;

		String select_sql = "SELECT * ";
		String query_sql = "FROM G_LINE " + where;

		try{
			Subs = LineModel.dao.paginate(page, limit, select_sql, query_sql);
		}catch (Exception e){
			setError(e.getMessage());
		}
		return Subs;
	}



	/**
	 * 增加供电线路信息
	 * @param no
	 * @param name
	 * @param type
	 * @return
	 */
	public static boolean isAdd(Map<String,String> params){
		Record record = new Record();
		record.set("line_id", Db.queryBigDecimal("select g_line_seq.nextval from dual").intValue());
		record.set("line_code", params.get("line_code").toString());
		record.set("line_name", params.get("line_name").toString());
		record.set("line_org_no", params.get("line_org_no").toString());
		record.set("line_subs_id", params.get("line_subs_id").toString());
		boolean isupadded = Db.save("g_line", "line_id",record);
		if(isupadded){
			return true;
		}
		return setError("你访问的供电线路已存在！");
	}

	/**
	 * 修改供电线路信息
	 * @param no
	 * @param name
	 * @param type
	 * @return
	 */
	public static boolean isUpdate(Map<String,String> params){
		Record record = new Record();
		record.set("line_id", params.get("line_id").toString());
		record.set("line_code", params.get("line_code").toString());
		record.set("line_name", params.get("line_name").toString());
		record.set("line_org_no", params.get("line_org_no").toString());
		record.set("line_subs_id", params.get("line_subs_id").toString());
		boolean isupadded = Db.update("g_line", "line_id",record);
		if(isupadded){
			return true;
		}
		return setError("您访问的供电线路不存在！");
	}

	/**
	 * 删除供电线路信息
	 * @param no
	 * @return
	 */
	public static boolean isDelete(Map<String,String> params){
		String no = params.get("line_code")==null?"":params.get("line_code").toString();
		String sql = "select * from g_line where line_code=?";
		LineModel lm = LineModel.dao.findFirst(sql, no);
		if (lm != null)
		{
			lm.delete();
			return true;
		}
		return setError("您访问的供电线路不存在！");
	}
}
